package gui_layer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;

@SuppressWarnings("serial")
public abstract class UniversalTableWindow extends JInternalFrame
{
	protected JTable table;
	private JPanel contentPane;
	protected String[] column_names = {};
	protected JPanel search_set_panel;
	protected JPanel display_panel;
	protected JPanel additional_button_panel;
	protected JPanel option_button_panel;


	/**
	 * Create the frame.
	 */
	public UniversalTableWindow()
	{
		setFont(new Font("Helvetica Neue", Font.PLAIN, 11));
		setResizable(true);
		setIconifiable(true);
		setClosable(true);
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		try
		{
			setFrameIcon(new ImageIcon(UniversalTableWindow.class.getResource("/img/icon.png")));
		}
		catch (Exception e)
		{	
		}
		setBounds(100, 100, 450, 300);
		
		contentPane = new JPanel();
		contentPane.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		make_north_panels();
		make_south_panels();
		make_table();
	}
	
	private void make_table()
	{
		table = new JTable();
		table.setFont(new Font("Helvetica Neue", Font.BOLD, 12));
		
		table.getTableHeader().setDefaultRenderer(new DefaultTableCellRenderer()
		{
			  {
				  float[] values = Color.RGBtoHSB(255, 104, 0, null);
				  setBackground(Color.getHSBColor(values[0], values[1], values[2]));
				  setOpaque(true);
				  setForeground(Color.WHITE);
				  this.setFont(new Font("Helvetica Neue", Font.BOLD, 12));
			  }
		});
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		table.setFont(new Font("Helvetica Neue", Font.PLAIN, 11));
		table.setFillsViewportHeight(true);
		table.setAutoCreateRowSorter(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getTableHeader().setReorderingAllowed(false);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.setBorder(new EmptyBorder(3, 6, 3, 6));
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBorder(new RoundedCornerBorder());
		scrollPane.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		contentPane.add(scrollPane, BorderLayout.CENTER);
	}
	
	private void make_north_panels()
	{
		JPanel north_panel = new JPanel();
		north_panel.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		contentPane.add(north_panel, BorderLayout.NORTH);
		north_panel.setLayout(new BorderLayout(0, 0));
		
		search_set_panel = new JPanel();
		search_set_panel.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		north_panel.add(search_set_panel, BorderLayout.WEST);
		
		display_panel = new JPanel();
		display_panel.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		north_panel.add(display_panel, BorderLayout.EAST);
	}
	
	private void make_south_panels()
	{
		JPanel south_panel = new JPanel();
		south_panel.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		contentPane.add(south_panel, BorderLayout.SOUTH);
		south_panel.setLayout(new BorderLayout(0, 0));
		
		additional_button_panel = new JPanel();
		additional_button_panel.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		south_panel.add(additional_button_panel, BorderLayout.WEST);
		
		option_button_panel = new JPanel();
		option_button_panel.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		south_panel.add(option_button_panel, BorderLayout.EAST);
	}
	
	protected void prepare_table(Object[][] filling, String[] columns)
	{
		column_names = columns;
		table.setModel(new MyTableModel(filling, columns));
	}
	

}
